package contactbook.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "account")
public class Account implements Serializable {
        
	private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    @Column(unique=true, nullable=false) 
    private String login;
    
    @Column(nullable = false)
    private String password;
    
    @Temporal(TemporalType.DATE)
    @Column(nullable = false)
    private Date dateCreated = new Date();
    
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "account", cascade = CascadeType.REMOVE)
    @OrderBy("name ASC")
    private Set<Group> groups = new HashSet<Group>();
    
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "account", cascade = CascadeType.REMOVE)
    @OrderBy("lastName ASC, firstName ASC")
    private Set<Contact> contacts = new HashSet<Contact>();
    
    public Account() {}
    
    public Account(String login, String password) {
            this.login = login;
            this.password = password;
    }

    public Set<Group> getGroups() {
            return groups;
    }
    
    public void  setGroups(Set<Group> groups) {
    	this.groups = groups;
    }

    public Set<Contact> getContacts() {
            return contacts;
    }
    
    public void  setContacts(Set<Contact> contacts) {
    	this.contacts = contacts;
    }

    public int getId() {
            return id;
    }

    public void setId(int id) {
            this.id = id;
    }

    public String getLogin() {
            return login;
    }

    public void setLogin(String login) {
            this.login = login;
    }

    public String getPassword() {
            return password;
    }

    public void setPassword(String password) {
            this.password = password;
    }
    
    public Date getDateCreated() {
    	return dateCreated;
    }
    
    public void setDateCreated(Date date) {
    	this.dateCreated = date;
    }
    
    public Group addGroup(Group group) {
            groups.add(group);
            group.setAccount(this);
            return group;
    }
    
    public Contact addContact(Contact contact) {
            contacts.add(contact);
            contact.setAccount(this);
            return contact;
    }
    
}